package com.jy.api.service.impl;

import com.jy.api.dao.StockProductDao;
import com.jy.api.domain.StockProduct;
import com.jy.api.service.StockProductService;
import com.jy.api.signUpReq.StockProductReq;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by xiaohua on 2019/4/19.
 */
@Service
public class StockProductServiceImpl implements StockProductService {
    @Autowired
    private StockProductDao productDao;

    @Override
    public List<StockProduct> findByTypeId(int typeId) {
        if (typeId == 0) {
            return productDao.findAllByEnabled(true);
        } else {
            return productDao.findAllByProductTypeIdAndEnabled(typeId, true);
        }
    }

    @Override
    @Transactional
    public void updateOrSave(StockProductReq req) {
        StockProduct product;
        if (req.getId() == 0) {
            product = new StockProduct();
            BeanUtils.copyProperties(req, product);
            product.setEnabled(true);
        } else {
            product = productDao.findOne(req.getId());
            BeanUtils.copyProperties(req, product);
        }
        productDao.save(product);
    }

    @Override
    public void deleteById(int id) {
        StockProduct product = productDao.findOne(id);
        product.setEnabled(false);
        productDao.save(product);
    }


}
